package com.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;

import com.baidu.location.demo.MainActivity;
import com.baidu.location.demo.R;
import com.entity.Friends;
import com.listview.MessageAdapter;
import com.listview.MessageList;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link MessageFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MessageFragment extends Fragment {
    private LinearLayout dataListView;
    private ListView  messageListView;
    public static List<MessageList> messageLists=null;
    private MessageAdapter  messageAdapter=null;
    public MessageFragment() {
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    public static MessageFragment newInstance() {
        return new MessageFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        messageLists=new ArrayList<>();
        dataListView=getActivity().findViewById(R.id.dataListView);
        new Thread(){
            @Override
            public void run() {
                super.run();
                try {
                    MainActivity.friendsListThread.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                for (Friends f:MainActivity.friends) {
                    MessageList messageList=new MessageList();
                    messageList.setId(f.getFriendId());
                    messageList.setHeadPortrait(f.getFriend().getHeadPortrait());
                    messageList.setName(f.getFriend().getName());
                    messageLists.add(messageList);
                }
                getActivity().runOnUiThread(() -> {
                    messageListView=getActivity().findViewById(R.id.messageListView);
                    messageAdapter=new MessageAdapter(getContext(),R.layout.messagelistlayout,messageLists,getActivity());
                    messageListView.setAdapter(messageAdapter);
                });
            }
        }.start();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_message, container, false);
    }

    @Override
    public void onStart() {
        super.onStart();
    }
}